package com.botarena.client.contest;

import java.util.List;

import com.botarena.client.SiteTokens;
import com.botarena.client.UIHelper;
import com.botarena.client.ui.StringHelper;
import com.botarena.shared.RankInfo;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RankingTop extends VerticalPanel {
	
	
	CellTable<RankInfo> rankingTable;
	
	public RankingTop() {
		setWidth("100%");
		Label title = new Label("Bot's top " + UIHelper.TOP_N);
		title.setStyleName("title2");
		

		rankingTable = new CellTable<RankInfo>();
		rankingTable.setWidth("100%");
		buildColumnModel(rankingTable);
		
		Anchor rankingFull = new Anchor("Full ranking", SiteTokens.hashToken(SiteTokens.RANKING));
		
		
		add(title);
		add(rankingTable);
		add(rankingFull);
	}
	
	public void setRankingData(List<RankInfo> data) {
		rankingTable.setRowCount(data.size());
		rankingTable.setRowData(data);
	}
	
	private void buildColumnModel(CellTable<RankInfo> rankingTable) {
		TextColumn<RankInfo> posCol = new TextColumn<RankInfo>() {
			@Override
			public String getValue(RankInfo pos) {
				return "" + (pos.pos + 1);
			}
		};
		rankingTable.addColumn(posCol, "POSITION");

		TextColumn<RankInfo> botNameCol = new TextColumn<RankInfo>() {
			@Override
			public String getValue(RankInfo pos) {
				return StringHelper.limitedLengthString(pos.botName, UIHelper.MAX_NAME_LEN );
			}
		};
		rankingTable.addColumn(botNameCol, "BOT NAME");

		TextColumn<RankInfo> userNameCol = new TextColumn<RankInfo>() {
			@Override
			public String getValue(RankInfo pos) {
				return StringHelper.limitedLengthString(pos.userName, UIHelper.MAX_NAME_LEN);
			}
		};
		rankingTable.addColumn(userNameCol, "USER NAME");

		TextColumn<RankInfo> rankCol = new TextColumn<RankInfo>() {
			@Override
			public String getValue(RankInfo pos) {
				return "" + pos.rank;
			}
		};
		rankingTable.addColumn(rankCol, "RANK");
	}
	
}
